<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-select v-model="queryParams.equipmentId" filterable placeholder="请选择">
          <el-option
            v-for="item in equipments"
            :key="item.label"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="误差结果" prop="errorResult">
        <el-input
          v-model="queryParams.errorResult"
          placeholder="请输入通道数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道数" prop="numOfCh">
        <el-input
          v-model="queryParams.numOfCh"
          placeholder="请输入通道数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备地址" prop="equipmentAddr">
        <el-input
          v-model="queryParams.equipmentAddr"
          placeholder="请输入设备地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道号" prop="channelNum">
        <el-input
          v-model="queryParams.channelNum"
          placeholder="请输入通道号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="温度状态" prop="stateName">
        <el-input
          v-model="queryParams.stateName"
          placeholder="请输入温度状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采集时间">
        <el-date-picker
          v-model="collectTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
        >清空</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >Excel导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" >
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备ID" align="center" prop="equipmentId" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" />
      <el-table-column label="设备型号" align="center" prop="equipmentSize" />
      <el-table-column label="通道数" align="center" prop="numOfCh" />
      <el-table-column label="设备地址" align="center" prop="equipmentAddr" />
      <el-table-column label="通道号" align="center" prop="channelNum" />
      <el-table-column label="温度状态" align="center" prop="stateName" />
      <el-table-column label="通道温度" align="center" prop="temperature" />
      <el-table-column label="PD" align="center" prop="pd" />
      <el-table-column label="UV" align="center" prop="uv" />
      <el-table-column label="电流/电压" align="center" prop="analog" />
      <el-table-column label="模拟温度" align="center" prop="simulationTemperature" />
      <el-table-column label="温度误差" align="center" prop="temperatureError" />
      <el-table-column label="误差结果" align="center" prop="errorResult" />
      <el-table-column label="采集时间" align="center" prop="collectTime" width="180" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改荧光模拟量温度监控数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备ID" prop="equipmentId">
          <el-input v-model="form.equipmentId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="通道数" prop="numOfCh">
          <el-input v-model="form.numOfCh" placeholder="请输入通道数" />
        </el-form-item>
        <el-form-item label="设备地址" prop="equipmentAddr">
          <el-input v-model="form.equipmentAddr" placeholder="请输入设备地址" />
        </el-form-item>
        <el-form-item label="通道号" prop="channelNum">
          <el-input v-model="form.channelNum" placeholder="请输入通道号" />
        </el-form-item>
        <el-form-item label="温度状态" prop="temState">
          <el-input v-model="form.stateName" placeholder="请输入温度状态" />
        </el-form-item>
        <el-form-item label="通道温度" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入通道温度" />
        </el-form-item>
        <el-form-item label="通道电流" prop="electricity">
          <el-input v-model="form.analog" placeholder="请输入通道电流" />
        </el-form-item>
        <el-form-item label="模拟温度" prop="simulationTemperature">
          <el-input v-model="form.simulationTemperature" placeholder="请输入模拟温度" />
        </el-form-item>
        <el-form-item label="温度误差" prop="temperatureError">
          <el-input v-model="form.temperatureError" placeholder="请输入温度误差" />
        </el-form-item>
        <el-form-item label="误差结果" prop="errorResult">
          <el-input v-model="form.errorResult" placeholder="请输入误差结果" />
        </el-form-item>
        <el-form-item label="采集时间" prop="collectTime">
          <el-input v-model="form.collectTime" placeholder="采集时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listData, getData, delData, addData, updateData, cleanHistoryData, getEquipmentList} from "@/api/analyse/data";
export default {
  name: "Data",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 荧光模拟量温度监控数据表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 设备名称
      equipments: [],
      collectTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        equipmentName: null,
        numOfCh: null,
        equipmentAddr: null,
        channelNum: null,
        stateName:null,
        temperature: null,
        pd: null,
        uv: null,
        analog: null,
        simulationTemperature: null,
        errorResult:null,
        temperatureError: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equipmentId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" }
        ],
        equipmentName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        equipmentAddr: [
          { required: true, message: "设备地址不能为空", trigger: "blur" }
        ],
        channelNum: [
          { required: true, message: "通道号不能为空", trigger: "blur" }
        ],
      }
    };

  },
  created() {
    this.equipmentList();
    this.getList();
  },
  methods: {
    getCurrentDate(offset = 0) {
      const now = new Date();
      now.setHours(now.getHours() - offset * 24); // 减去相应小时数
      return this.formatDateTime(now);
    },
    formatDateTime(date) {
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      const hours = date.getHours().toString().padStart(2, '0');
      const minutes = date.getMinutes().toString().padStart(2, '0');
      const seconds = date.getSeconds().toString().padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    /**获取设备名称列表*/
    equipmentList(){
      getEquipmentList().then(res=>{
        this.queryParams.equipmentId = res.data[0].equipmentId;
        res.data.forEach(result=>{
          this.equipments.push({
            label:result.equipmentName,
            value:result.equipmentId
          });
        })
      })
    },
    /** 查询荧光模拟量温度监控数据列表 */
    getList() {
      this.loading = true;
      listData(this.addDateRange(this.queryParams, this.collectTime)).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentId: null,
        equipmentName: null,
        numOfCh: null,
        equipmentAddr: null,
        channelNum: null,
        stateName:null,
        temperature: null,
        analog: null,
        simulationTemperature: null,
        temperatureError: null,
        errorResult: null,
        collectTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.collectTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模拟量监控数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模拟量监控数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除模拟量监控数据编号为"' + ids + '"的数据项？').then(function() {
        return delData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('analyse/data/export', {
        ...this.queryParams
      }, `历史数据_${new Date().getTime()}.xlsx`)
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal.confirm('请确认是否已经保存历史数据？').then(function() {
        return cleanHistoryData();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("清空成功");
      }).catch(() => {});
    }
  }
};
</script>
